package com.zjdiepu.www.manage.view;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.DataDicType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.commpent.ViewScopeControllerHelper;
import com.zjdiepu.www.ibiz.cache.ICachedDictionaryService;
import com.zjdiepu.www.ibiz.dz.IDzPickCarService;
import com.zjdiepu.www.ibiz.dz.IDzRegistrationService;
import com.zjdiepu.www.model.dz.dto.DzPickCar;
import com.zjdiepu.www.model.dz.dto.DzRegistration;
import com.zjdiepu.www.model.dz.ext.DzRegistrationExt;
import com.zjdiepu.www.model.yw.dto.YwBusinessCar;
import com.zjdiepu.www.model.yw.response.YwBusinessDetail;
import com.zjdiepu.www.util.ExceptionHelper;

/**
 * 车辆信息Controller层<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年05月09日 19时30分<br/>
 * @since 1.0<br/>
 */
@Controller
@RequestMapping(value= "YwBusinessCarViewController", produces="text/html;charset=UTF-8")
@Api(tags = {"车辆信息"}, produces="text/html;charset=UTF-8")
public class YwBusinessCarViewController extends BaseController {
	@Resource
    private ViewScopeControllerHelper viewScopeControllerHelper;
    @Resource
    private ICachedDictionaryService cachedDictionaryService;
    @Resource
    private IDzRegistrationService dzRegistrationService;
    @Resource
    private IDzPickCarService dzPickCarService;
	
	/**
     * @author xiongxiaotun <br/>
     * @Title: registrationPlatesDetail  <br/>
     * @Description: 车牌登记详情 <br/>
     * @param model
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "车牌登记详情", response = Boolean.class, httpMethod="GET", tags={"车牌登记"})
    @RequestMapping(value = "registrationPlatesDetail", method = RequestMethod.GET)
    public String registrationPlatesDetail(Model model, String id) {
    	if (StringUtils.isBlank(id)) {
			ExceptionHelper.thow(ResultFlagType.PARAM_EMPTY);
		}
    	model.addAttribute("carCategoryType", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.CAR_CATEGORY,"请选择..."));
    	model.addAttribute("carColorType", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.CAR_COLOR,"请选择..."));
    	
    	viewScopeControllerHelper.preparedNowTime(model);
    	YwBusinessDetail ywBusinessDetail = viewScopeControllerHelper.preparedBusiness(id, model);
    	YwBusinessCar car = viewScopeControllerHelper.preparedCar(id,ywBusinessDetail.getCustomerId(), model);
    	if(car != null) {
    		viewScopeControllerHelper.preparedLoan(id, car.getId(), model, car.getCarType());
    	}
    	if(ywBusinessDetail != null) {
    		viewScopeControllerHelper.preparedHistoryBusiness(ywBusinessDetail.getCustomerNo(), model);
    	}
    	DzRegistrationExt dzRegistrationExt = new DzRegistrationExt();
    	dzRegistrationExt.setBaseWhere();
    	dzRegistrationExt.setBusiId(id);
    	dzRegistrationExt.setRows(1);
    	List<DzRegistration> dzRegistrations = dzRegistrationService.selectByExt(dzRegistrationExt);
    	if(CollectionUtils.isNotEmpty(dzRegistrations)) {
    		DzRegistration dzRegistration = dzRegistrations.get(0);
    		model.addAttribute("dzRegistration", dzRegistration);
    		
    		viewScopeControllerHelper.preparedRegin(model,dzRegistration.getProvinceRegistration(),dzRegistration.getAreaRegistration());
    	} else {
    		viewScopeControllerHelper.preparedRegin(model,null,null);
    	}
    	
    	
    	return "system/loaning/registrationPlatesDetail";
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: liftConfirmationDetail  <br/>
     * @Description: 确认提车详情 <br/>
     * @param model
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "确认提车详情", response = Boolean.class, httpMethod="GET", tags={"确认提车"})
    @RequestMapping(value = "liftConfirmationDetail", method = RequestMethod.GET)
    public String liftConfirmationDetail(Model model, String id) {
    	if (StringUtils.isBlank(id)) {
			ExceptionHelper.thow(ResultFlagType.PARAM_EMPTY);
		}
    	model.addAttribute("carCategoryType", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.CAR_CATEGORY,"请选择..."));
    	model.addAttribute("carColorType", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.CAR_COLOR,"请选择..."));
    	
    	
    	viewScopeControllerHelper.preparedNowTime(model);
    	YwBusinessDetail ywBusinessDetail = viewScopeControllerHelper.preparedBusiness(id, model);
    	YwBusinessCar car = viewScopeControllerHelper.preparedCar(id,ywBusinessDetail.getCustomerId(), model);
    	if(car != null) {
    		viewScopeControllerHelper.preparedLoan(id, car.getId(), model, car.getCarType());
    	}
    	if(ywBusinessDetail != null) {
    		viewScopeControllerHelper.preparedHistoryBusiness(ywBusinessDetail.getCustomerNo(), model);
    	}
    	
    	viewScopeControllerHelper.preparedCarModel(model);
    	viewScopeControllerHelper.preparedAttachmentEnum(model);
    	
    	DzPickCar dzPickCar = new DzPickCar();
    	dzPickCar.setBusiId(id);
    	dzPickCar.setDpStatus(DpStatus.NORMAL.getCode());
    	dzPickCar = dzPickCarService.selectRefined(dzPickCar);
    	if(null != dzPickCar) {
    		model.addAttribute("dzPickCar", dzPickCar);
    	}
		
    	return "system/loaning/liftConfirmationDetail";
    }

}
